package com.organization

import grails.test.*
import grails.converters.JSON

class DataControllerUnitTests extends ControllerUnitTestCase {

    DataControllerUnitTests(){
        super(DataController)
    }

    protected void setUp() {
        super.setUp()
    }

    protected void tearDown() {
        super.tearDown()
    }

    void testIndex() {
        this.controller.index()
        assertEquals "",mockResponse.contentAsString
    }

    void testGetTitle(){
        //mock required method
        def mockControl = mockFor(DataService)
        mockControl.demand.getOrganizationInfo(1..1) {->
            return [success:"true",name:"orgName"]
        }
        controller.dataService = mockControl.createMock()
        //call action in controller
        this.controller.getTitle()
        //parse the JSON
        def controllerResponse = mockResponse.contentAsString
        def jsonResult = JSON.parse(controllerResponse)
        //navigate the JSON as an object
        assertNotNull "Missing success field", jsonResult.success
        assertNotNull "Missing title field", jsonResult.name
    }

    void testGetElement(){
        def mockControl = mockFor(DataService)
        mockControl.demand.getElement(1..1) {params->
            return [success:"true"]
        }
        controller.dataService = mockControl.createMock()
        this.controller.params.node = "node"
        this.controller.params.nodeBdId = "nodeDbId"
        this.controller.getElement()
        def controllerResponse = mockResponse.contentAsString
        def jsonResult = JSON.parse(controllerResponse)
        assertNotNull "Missing success field", jsonResult.success
    }

    void testUpdate(){
        def mockControl = mockFor(DataService)
        mockControl.demand.updateElement(1..1) {params->
            return [success:"true"]
        }
        controller.dataService = mockControl.createMock()
        this.controller.params.node = "node"
        this.controller.params.nodeBdId = "nodeDbId"
        this.controller.update()
        def controllerResponse = mockResponse.contentAsString
        def jsonResult = JSON.parse(controllerResponse)
        assertNotNull "Missing success field", jsonResult.success
    }

    void testAdd(){
        def mockControl = mockFor(DataService)
        mockControl.demand.addElement(1..1) {params->
            return [success:"true"]
        }
        controller.dataService = mockControl.createMock()
        this.controller.params.parentNodeId = "parentNodeId"
        this.controller.add()
        def controllerResponse = mockResponse.contentAsString
        def jsonResult = JSON.parse(controllerResponse)
        assertNotNull "Missing success field", jsonResult.success
    }

    void testDelete(){
        def mockControl = mockFor(DataService)
        mockControl.demand.deleteElement(1..1) {params->
            return [success:"true"]
        }
        controller.dataService = mockControl.createMock()
        this.controller.params.node = "node"
        this.controller.params.nodeBdId = "nodeDbId"
        this.controller.delete()
        def controllerResponse = mockResponse.contentAsString
        def jsonResult = JSON.parse(controllerResponse)
        assertNotNull "Missing success field", jsonResult.success
    }

}
